import { saveToStorage, loadFromStorage } from '@/js/storage'

const SEARCH_HISTORY_KEY = 'search_history'
const SEARCH_HISTORY_MAX_LENGTH = 5

// 搜索历史

const saveSearchHistory = function (searchContent) {
  const searchHistory = loadFromStorage(SEARCH_HISTORY_KEY, [])
  // TODO searchContent 放到 searchHistory中
  insertArray(
    searchHistory,
    searchContent,
    (item) => {
      return item === searchContent
    },
    SEARCH_HISTORY_MAX_LENGTH
  )
  saveToStorage(SEARCH_HISTORY_KEY, searchHistory)
}

const loadSearchHistory = function () {
  return loadFromStorage(SEARCH_HISTORY_KEY, [])
}

const deleteSearchHistory = function (item) {
  let historyList = loadFromStorage(SEARCH_HISTORY_KEY, [])
  historyList.splice(historyList.indexOf(item), 1)
  saveToStorage(SEARCH_HISTORY_KEY, historyList)
}

const clearSearchHistory = function () {
  saveToStorage(SEARCH_HISTORY_KEY, undefined)
}

// 插入数组
function insertArray(arr, item, compareFn, maxLen) {
  if (compareFn) {
    const index = arr.findIndex(compareFn)
    if (index >= 0) {
      // 数组中已存在
      // 删除旧数据
      arr.splice(index, 1)
    }
  }
  arr.unshift(item)
  if (maxLen && arr.length > maxLen) {
    arr.pop()
  }
}

// 播放列表
const PLAY_LIST_KEY = 'play_list'

const savePlayList = function (songItem) {
  const playList = loadFromStorage(PLAY_LIST_KEY, [])
  insertArray(playList, songItem, (item) => {
    return item.id === songItem.id
  })
  saveToStorage(PLAY_LIST_KEY, playList)
}

const loadPlayList = function () {
  return loadFromStorage(PLAY_LIST_KEY, [])
}

const deletePlayList = function (item) {
  let playList = loadFromStorage(PLAY_LIST_KEY, [])
  const index = playList.findIndex((i) => {
    return i.id === item.id
  })
  playList.splice(index, 1)
  saveToStorage(PLAY_LIST_KEY, playList)
}

const clearPlayList = function () {
  saveToStorage(PLAY_LIST_KEY, undefined)
}

// 最近播放
const LAST_PLAY_LIST_KEY = 'last_play_list'

const saveLastPlayList = function (lastPlayItem) {
  const lastPlayList = loadFromStorage(LAST_PLAY_LIST_KEY, [])
  insertArray(lastPlayList, lastPlayItem, (item) => {
    return item.id === lastPlayItem.id
  })
  saveToStorage(LAST_PLAY_LIST_KEY, lastPlayList)
}

const loadLastPlayList = function () {
  return loadFromStorage(LAST_PLAY_LIST_KEY, [])
}

const deleteLastPlayList = function (item) {
  let lastPlayList = loadFromStorage(LAST_PLAY_LIST_KEY, [])
  const index = lastPlayList.findIndex((i) => {
    return i.id === item.id
  })
  lastPlayList.splice(index, 1)
  saveToStorage(LAST_PLAY_LIST_KEY, lastPlayList)
}

const clearLastPlayList = function () {
  saveToStorage(LAST_PLAY_LIST_KEY, undefined)
}

// 我喜欢的音乐
const MY_LOVE_SONG_LIST = 'my_love_song_list'

const saveMyLoveSongList = function (loveSongItem) {
  const myLoveSongList = loadFromStorage(MY_LOVE_SONG_LIST, [])
  insertArray(myLoveSongList, loveSongItem, (item) => {
    return item.id === loveSongItem.id
  })
  saveToStorage(MY_LOVE_SONG_LIST, myLoveSongList)
}

const loadMyLoveSongList = function () {
  return loadFromStorage(MY_LOVE_SONG_LIST, [])
}

const deleteMyLoveSongList = function (item) {
  let myLoveSongList = loadFromStorage(MY_LOVE_SONG_LIST, [])
  const index = myLoveSongList.findIndex((i) => {
    return i.id === item.id
  })
  myLoveSongList.splice(index, 1)
  saveToStorage(MY_LOVE_SONG_LIST, myLoveSongList)
}

export { saveSearchHistory, loadSearchHistory, deleteSearchHistory, clearSearchHistory, savePlayList, loadPlayList, deletePlayList, clearPlayList, saveLastPlayList, loadLastPlayList, deleteLastPlayList, clearLastPlayList, saveMyLoveSongList, loadMyLoveSongList, deleteMyLoveSongList }
